/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.persistence;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.KeyResponse;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.Modem;
import br.com.jr2tecnologia.model.entity.SmsReceived;
import br.com.jr2tecnologia.model.repository.SmsReceivedRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Query;

/**
 *
 * @author ronildo
 */
public class SmsReceivedDAO extends GenericDAO<SmsReceived, SmsReceivedRepository> implements SmsReceivedRepository {
    
    private static final Logger logger = LoggerFactory.getLogger();

    public List<SmsReceived> list(SmsReceived smsReceived){
        try{
            KeyResponse keyResponse = smsReceived.getKeyResponse();
            Modem modem = smsReceived.getModem();
            Login login = smsReceived.getLogin();
            String hql = "select smsReceived from SmsReceived as smsReceived ";
            hql+=" left outer join smsReceived.keyResponse as keyResponse";
            hql+=" left outer join smsReceived.modem as modem";
            hql+=" left outer join smsReceived.login as login";
            hql+=" where smsReceived.pkSmsReceived = smsReceived.pkSmsReceived";

            if(smsReceived.getSender() != null && !smsReceived.getSender().equals(""))
                hql+=" and smsReceived.sender = :sender";
            if(keyResponse != null && keyResponse.getPkKeyResponse() != null)
                hql+=" and keyResponse.pkKeyResponse = :pkKeyResponse";
            if(modem != null && modem.getPkModem() != null)
                hql+=" and modem.pkModem = :pkModem";
            if(login != null && login.getPkLogin() != null)
                hql+=" and login.pkLogin = :pkLogin";
            hql+=" order by smsReceived.datetimeReceived";

            Query query = em.createQuery(hql);
            if(smsReceived.getSender() != null && !smsReceived.getSender().equals(""))
                query.setParameter("sender", smsReceived.getSender());
            if(keyResponse != null && keyResponse.getPkKeyResponse() != null)
                query.setParameter("pkKeyResponse", keyResponse.getPkKeyResponse());
            if(modem != null && modem.getPkModem() != null)
                query.setParameter("pkModem", modem.getPkModem());
            if(login != null && login.getPkLogin() != null)
                query.setParameter("pkLogin", login.getPkLogin());

            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list sms send",e);
            throw new DAOException(e);
        }
    }

    public List<SmsReceived> listAll(){
        try{
            return em.createNamedQuery("SmsReceived.findAll").getResultList();
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }
}
